﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Text;

public partial class CampaignPeriodControl : System.Web.UI.UserControl
{
    public DateTime StartDate
    {
        get
        {
            if (ViewState["StartDate"] != null)
                return (DateTime)ViewState["StartDate"];
            return DateTime.Now;
        }
        set
        {
            ViewState["StartDate"] = value;
        }
    }

    public string SelectTimes
    {
        get
        {
            return "0123456789";
        }
    }

    public int VersionsCount
    {
        get
        {
            if (ViewState["VersionsCount"] != null)
                return (int)ViewState["VersionsCount"];
            return 1;
        }
        set
        {
            ViewState["VersionsCount"] = value;
        }
    }

    protected string SelectVersions
    {
        get
        {
            int count = VersionsCount;
            string versons = " ";
            char index = 'A';
            for (int i = 0; i < count; i++)
            {
                versons += index;
                index++;
            }
            return versons;
        }
    }

    protected int Colspan1
    {
        get
        {
            return DateTime.DaysInMonth(StartDate.Year, StartDate.Month) - StartDate.Day + 1;
        }
    }

    protected int Colspan2
    {
        get
        {
            return EndDate.Day;
        }
    }

    protected string DayHtml
    {
        get
        {
            return Formater.GetCampaignPeriodDaysHTML(StartDate, EndDate);
        }
    }

    public int TotalTimes
    {
        get
        {
            int times = 0;
            
            foreach (RepeaterItem item in this.CampaignPeriodRepeater.Items)
            {
                TextBox txtTimes = item.FindControl("txtTimes") as TextBox;
                if (txtTimes.Text.Trim() != string.Empty)
                {
                    times += int.Parse(txtTimes.Text);
                }
            }
            return times;
        }
    }

    public string CampaignPeriodString
    {
        get
        {
            StringBuilder sb = new StringBuilder(31);
            foreach (RepeaterItem item in this.CampaignPeriodRepeater.Items)
            {
                TextBox txtTimes = item.FindControl("txtTimes") as TextBox;
                string timesText = txtTimes.Text.Trim() == string.Empty ? "0" : txtTimes.Text.Trim();
                DropDownList VersonDropDownList = item.FindControl("VersonDropDownList") as DropDownList;
                sb.Append("|" + timesText + VersonDropDownList.SelectedValue);
            }
            sb.Remove(0, 1);
            return sb.ToString();
        }
        set
        {
            if (string.IsNullOrEmpty(value))
                return;
            string[] cps = value.Split('|');
            for (int i = 0; i < cps.Length; i++)
            {
                RepeaterItem item = this.CampaignPeriodRepeater.Items[i];
                TextBox txtTimes = item.FindControl("txtTimes") as TextBox;
                DropDownList VersonDropDownList = item.FindControl("VersonDropDownList") as DropDownList;
                string timesText = cps[i].Substring(0, 1);
                txtTimes.Text = timesText == "0" ? string.Empty : timesText;
                VersonDropDownList.SelectedValue = cps[i].Substring(1, 1);
            }
        }
    }

    public DateTime EndDate
    {
        get
        {
            if (ViewState["EndDate"] == null)
                return StartDate.AddMonths(1);
            return (DateTime)ViewState["EndDate"];
        }
        set
        {
            if (value < StartDate)
            {
                throw new ArgumentOutOfRangeException("结束时间不能小于开始时间");
            }
            ViewState["EndDate"] = value;
        }
    }

    public void DataBind()
    {
        TimeSpan span = EndDate - StartDate;
        int days = span.Days + 1;

        DataTable dt = new DataTable();
        dt.Columns.Add("Times");
        dt.Columns.Add("Versions");
        for (int i = 0; i < days; i++)
        {
            DataRow dr = dt.NewRow();
            //dr["Times"] = SelectTimes;
            dr["Versions"] = SelectVersions;
            dt.Rows.Add(dr);
        }
        this.CampaignPeriodRepeater.DataSource = dt;
        this.CampaignPeriodRepeater.DataBind();
    }

    public event EventHandler TimesTextChanged;

    protected void CampaignPeriodRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item ||
            e.Item.ItemType == ListItemType.AlternatingItem)
        {
            if (VersionsCount <= 1)
            {
                HtmlGenericControl VersionsSpan = e.Item.FindControl("VersionsSpan") as HtmlGenericControl;
                VersionsSpan.Visible = false;
            }
        }
    }
}